package dao

import (
	"LibraryManagementSys/gorm/dbm"
	"LibraryManagementSys/gorm/query"
	"gorm.io/gen"
)

var UserDao = &userDao{}

type userDao struct{}

// Insert 添加用户
// @receiver userDao
// @return   error
func (userDao) Insert(user *dbm.User) error {
	u := query.Use(db).User
	return u.Create(user)
}

// DeleteByID 根据 ID 删除用户
// @receiver userDao
// @return   gen.ResultInfo
// @return   error
func (userDao) DeleteByID(userID int) (gen.ResultInfo, error) {
	return query.User.Delete(&dbm.User{ID: userID})
}

// Update 修改用户信息
// @receiver userDao
// @return   gen.ResultInfo
// @return   error
func (userDao) Update(user *dbm.User) (gen.ResultInfo, error) {
	u := query.User
	return u.Where(u.ID.Eq(user.ID)).Updates(user)
}

// GetAllUser 查询所有用户
// @receiver userDao
// @return   userList
// @return   err
func (userDao) GetAllUser() (userList []*dbm.User, err error) {
	return query.User.Find()
}

// LoadUserByUsername 根据用户名查找用户信息
// @receiver userDao
// @return   *dbm.User
// @return   error
func (userDao) LoadUserByUsername(username string) (*dbm.User, error) {
	u := query.User
	return u.Where(u.Username.Eq(username)).First()
}
